/**
 * 添加地址列表
 * addressList 添加地址or修改编辑地址
 */
class addAddress extends eui.Component{
    private return:eui.Rect;
    private add:eui.Button;
    private userfocus:eui.Label;
    private userName:eui.EditableText;
    private phonefocus:eui.Label;
    private phoneNum:eui.EditableText;
    private province:eui.EditableText;
    private city:eui.EditableText;
    private area:eui.EditableText;
    private address:eui.EditableText;
    private default:eui.Image;
    private isDefault:eui.Rect;
    private setAddress:eui.Rect;

    private _node:string = "";
    private _userName;
    private _phoneNum;
    private _province;
    private _city;
    private _area;
    private _address;
    private _isDefault;
    private _id;
    private _toast:Toast = new Toast();
    public constructor(node:string="add",userName:string="",phoneNum:number=0,province:string="",city:string="",area:string="",address:string="",isDefault:number=0,id:number=0){
        super();
       // this.cacheAsBitmap =true;
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "addAddressSkin"; 
        this.reset(node,userName,phoneNum,province,city,area,address,isDefault,id);
    }
    private createCompleteEvent():void{
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        this.inIt();
    }

    private inIt(){
        this.return.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            this.parent.removeChild(this);
        },this);
        this.isDefault.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            this._isDefault = this._isDefault == 0 ? 1 : 0;
            if(this._isDefault == 0){
                this.default.visible = true;
            }else{
                this.default.visible = false;
            }
        },this);
        this.userfocus.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            this.userName.setFocus();
        },this);
        this.phonefocus.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            this.phoneNum.setFocus();
        },this);
        this.setAddress.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            let roll = new pcSelector(this._province,this._city,this._area);
            this.addChild(roll);
            roll.addEventListener("address",(evt:egret.Event)=>{
                var obj = evt.data;
                this._province  = obj.pro;
                this._city = obj.city;
                this._area = obj.area;
                this.province.text = obj.pro;
                this.city.text = obj.city;
                this.area.text = obj.area;
            },this);
        },this);
        this.phoneNum.addEventListener("change",this.judgeNum,this);
        this.add.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            this._userName = this.userName.text;
            this._phoneNum = this.phoneNum.text;
            this._province = this.province.text;
            this._city = this.city.text;
            this._area = this.area.text;
            this._address = this.address.text;
            if(this._userName.length == 0 || this._phoneNum == 0 || this._province == 0 || this._city == 0 || this._area == 0 || this._address == 0){
                this._toast.show("请填写完整");
                this.addChild(this._toast);
            }else{
                let dataPost:any;
                dataPost = {
                    "userName":this._userName,
                    "province":this._province,
                    "city":this._city,
                    "area":this._area,
                    "address":this._address,
                    "phoneNum":this._phoneNum,
                    "choice":this._isDefault
                }
                if(this._node == "edit"){
                    dataPost.id = this._id;
                }
                socket.request("editAddress",dataPost);
                this.parent.removeChild(this);
            }
        },this);
    }

    public reset(node:string="add",userName:string="",phoneNum:number=0,province:string="",city:string="",area:string="",address:string="",isDefault:number=0,id:number=0):void{
        this._node = node;
        if(node == "add"){
            this.add.label = "添加地址";
        }else{
            this.add.label = "确认";
        }
        this._userName = userName;
        this._phoneNum = phoneNum;
        this._province = province;
        this._city = city;
        this._area = area;
        this._address = address;
        this._isDefault = isDefault;
        this._id = id;
        this.userName.text = this._userName;
        this.phoneNum.text = this._phoneNum;
        this.province.text = this._province;
        this.city.text = this._city;
        this.area.text = this._area;
        this.address.text = this._address;
        if(this._isDefault == 0){
            this.default.visible = true;
        }else{
            this.default.visible = false;
        }
    }
    private judgeNum():void{
        let value = this.phoneNum.text;
        value = value.replace(/[^0-9]/g,'');
        this.phoneNum.text = value;
    }
}